package routers

import (
	"gin-mvc/middleware"
	"gin-mvc/pkg/exceptions"
	"gin-mvc/routers/validators"
	"github.com/gin-gonic/gin"
	"net/http"
)

func InitRouter() *gin.Engine {
	r := gin.New()
	r.Use(gin.Logger(), middleware.Recover())

	// 注册验证器
	validators.RegisterValidator()

	// 未知调用方式
	r.NoMethod(func(c *gin.Context) {
		c.JSON(http.StatusNotFound, gin.H{
			"code":    exceptions.NotFound,
			"message": "请求method错误",
		})
	})

	// 未知路由出
	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusNotFound, gin.H{
			"code":    exceptions.NotFound,
			"message": exceptions.GetMsg(exceptions.NotFound),
		})
	})

	// 注册测试路由
	InitTagRouter(r)
	InitTestRouter(r)

	return r
}
